package org.springblade.weixin.controller;


import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaUserService;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.RoleConstant;
import org.springblade.resource.feign.IOssClient;
import org.springblade.resource.oss.BladeFileWithType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.nio.file.Files;
import java.util.Map;

@NonDS
@RestController
@AllArgsConstructor
@RequestMapping("miniapp/qr-code")
@Api(value = "微信小程序接口", tags = "小程序接口")
public class MiniappQrCodeController {

	private final WxMaService wxMaService;

	private final IOssClient ossClient;

	@SneakyThrows
	@GetMapping("/get")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "生成小程序码", notes = "生成小程序码")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "scene", value = "场景参数", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "page", value = "页面路径", paramType = "query", dataType = "string")
	})
	R<BladeFileWithType> getQrCode(@ApiIgnore @RequestParam Map<String, Object> param) {
		String scene = MapUtil.getStr(param, "scene", StrUtil.EMPTY);
		String page = MapUtil.getStr(param, "page", StrUtil.EMPTY);

		WxMaQrcodeService qrcodeService = wxMaService.getQrcodeService();
		File qrFile = qrcodeService.createWxaCodeUnlimit(scene, page);

		MultipartFile file = new MockMultipartFile("file", qrFile.getName(), null, Files.newInputStream(qrFile.toPath()));


		// 将File转换成MultipartFile
//		DiskFileItemFactory factory = new DiskFileItemFactory(16, null);
//		FileItem fileItem = factory.createItem(qrFile.getName(), "text/plain", true, qrFile.getName());
//		MultipartFile multipartFile = new CommonsMultipartFile(fileItem);

		R result = ossClient.putFile(file);
		FileUtil.del(qrFile);
		return result;
	}

}
